package com.liuyanban.action.user;

import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.liuyanban.action.base.BaseAction;
import com.liuyanban.domain.user.Privilege;
import com.liuyanban.domain.user.User;
import com.liuyanban.service.user.LoginService;
import com.liuyanban.service.user.PrivilegeService;

@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends BaseAction<User> {
	@Resource(name = "loginService")
	private LoginService loginService;

	@Resource(name = "privilegeService")
	private PrivilegeService privilegeService;

	public String authentication() {
		User user = this.loginService.authentication(this.getModel()
				.getUsername(), this.getModel().getUserpassword());
		if (user == null) {
			this.addActionError("用户名或者密码错误");
			return "login";
		} else {
			Collection<Privilege> functions = this.privilegeService
					.getFunctionTreeByUid(user.getUserid());
			this.getSession().setAttribute("user", user);// 把该用户能够访问到的功能权限放入到session中
			this.getSession().setAttribute("functions", functions);
			return "chain";
		}

	}
}
